package com.tdd.api.server.base.returns;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

/**
 * @author: TDD
 * @Description:统一返回格式(泛型)
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@ApiModel(value = "ResponseEntity")
@Data
@Setter
@Getter
public class ResponseEntity<T> {

    @ApiModelProperty(value = "返回状态码",example = "200")
    private Integer code;

    @ApiModelProperty(value = "返回信息",example = "成功")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private T data;

    public  ResponseEntity(){
    }

    public ResponseEntity(RespCode rc, T data) {
        this.code = rc.getCode();
        this.message = rc.getMessage();
        this.data = data;
    }

    public  ResponseEntity(RespCode rc){
        this.code = rc.getCode();
        this.message = rc.getMessage();
    }

    public  ResponseEntity(Integer code, String msg){
        this.code = code;
        this.message = msg;
    }

    public ResponseEntity success(RespCode respCode, T data) {
        return new ResponseEntity(respCode,data);
    }

    public static <T> ResponseEntity<T>  success(T data) {
        return new ResponseEntity(RespCode.SUCCESS,data);
    }

    public static <T> ResponseEntity<T> success() {
        return new ResponseEntity(RespCode.SUCCESS);
    }

    public static <T> ResponseEntity<T> serverError(T data) {
        return new ResponseEntity(RespCode.SERVER_ERROR,data);
    }

    public static <T> ResponseEntity<T> serverError() {
        return new ResponseEntity(RespCode.SERVER_ERROR);
    }

    public static <T> ResponseEntity<T> failure(RespCode respCode, T data) {
        return new ResponseEntity(respCode,data);
    }

    public static <E> ResponseEntity<E> buildFailure(Integer  code, String msg) {
        return new ResponseEntity(code,msg);
    }

    public static <E> ResponseEntity<E> buildFailure(RespCode respCode, String msg) {
        return new ResponseEntity(respCode,msg);
    }


    public static <T> ResponseEntity<T> failure(RespCode respCode) {
        return new ResponseEntity(respCode);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
